package com.zh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.dao.OrderMapper;
import com.zh.po.Order;
import com.zh.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ふわふわ
 * @version 1.0
 * @description:
 * @date 2021/4/15 20:49
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public PageInfo<Order> page(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList = orderMapper.getAll();
        if (orderList.size()>0){
            return new PageInfo<>(orderList);
        }
        return null;
    }

    @Override
    public int cancelOrder(Integer orderId) {
        Order order1 = orderMapper.selectByPrimaryKey(orderId);
        order1.setOrderState(-1);
        return orderMapper.updateByPrimaryKeySelective(order1);
    }

    @Override
    public int confirmOrder(Integer orderId) {
        Order order1 = orderMapper.selectByPrimaryKey(orderId);
        order1.setOrderState(1);
        return orderMapper.updateByPrimaryKeySelective(order1);
    }

    @Override
    public int confirmReceipt(Integer orderId) {
        Order order1 = orderMapper.selectByPrimaryKey(orderId);
        order1.setOrderState(2);
        return orderMapper.updateByPrimaryKeySelective(order1);
    }

    @Override
    public int refund(Integer orderId) {
        Order order1 = orderMapper.selectByPrimaryKey(orderId);
        order1.setOrderState(3);
        return orderMapper.updateByPrimaryKeySelective(order1);
    }

    @Override
    public int accessRefund(Integer orderId) {
        Order order1 = orderMapper.selectByPrimaryKey(orderId);
        order1.setOrderState(5);
        return orderMapper.updateByPrimaryKeySelective(order1);
    }

    @Override
    public int backRefund(Integer orderId) {
        Order order1 = orderMapper.selectByPrimaryKey(orderId);
        order1.setOrderState(1);
        return orderMapper.updateByPrimaryKeySelective(order1);
    }

    @Override
    public int backMoney(Integer orderId) {
        Order order1 = orderMapper.selectByPrimaryKey(orderId);
        order1.setOrderState(5);
        return orderMapper.updateByPrimaryKeySelective(order1);
    }

    @Override
    public int addOrder(Order order) {
       return orderMapper.insertSelective(order);
    }

    @Override
    public Order findOrderById(Integer orderId) {
        return orderMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public int pay(Integer orderId) {
        Order order1 = orderMapper.selectByPrimaryKey(orderId);
        order1.setOrderState(0);
        return orderMapper.updateByPrimaryKeySelective(order1);
    }

    @Override
    public Order getOrderById(Integer orderId) {
        return orderMapper.getOrderById(orderId);
    }

    @Override
    public int updateAddress(Order order) {
        return orderMapper.updateByPrimaryKeySelective(order);
    }
}
